package city.spring.modules.medicalstation.drugdeposit.controller;

import city.spring.modules.medicalstation.drugdeposit.entity.DrugdepositEntity;
import city.spring.modules.medicalstation.drugdeposit.service.DrugdepositService;
import city.spring.modules.medicalstation.drugdeposit.vo.DrugdepositVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Controller：医护站药品缴存
 *
 * @author 谢金婷
 * @date 2020-10-13 11:44:36
 */
@RestController
@RequestMapping("/drugdeposit")
public class DrugdepositController extends ApiController {
    private final DrugdepositService drugdepositService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<DrugdepositEntity, ?>> entityOrderFields;

    public DrugdepositController(DrugdepositService drugdepositService) {
        this.drugdepositService = drugdepositService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(DrugdepositEntity::getId);
        entityOrderFields.add(DrugdepositEntity::getOldmanId);
        entityOrderFields.add(DrugdepositEntity::getDepositdate);
        entityOrderFields.add(DrugdepositEntity::getDepositmethod);
        entityOrderFields.add(DrugdepositEntity::getRegistor);

    }

    /**
     * 获取全部的 <strong>医护站药品缴存</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, DrugdepositEntity entity) {
        LambdaQueryChainWrapper<DrugdepositEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>医护站药品缴存</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault @SortDefault.SortDefaults(@SortDefault(sort = "id", direction = Sort.Direction.DESC)) Pageable pageable, DrugdepositVO entity, Date[] depositdate) {
//        LambdaQueryChainWrapper<DrugdepositEntity> lambdaQuery = buildLambdaQuery(entity);
//        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
//        Page<DrugdepositEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
//        return success(page);
        QueryWrapper<? extends DrugdepositEntity> queryWrapper = buildQuery(entity);
        if (depositdate != null && depositdate.length == 2) {
            queryWrapper.ge("t1.depositdate", depositdate[0]);
            queryWrapper.le("t1.depositdate", depositdate[1]);
        }
        return success(drugdepositService.listPage(MyBatisUtils.toPage(pageable), queryWrapper));
    }

    /**
     * 获取一个 <strong>医护站药品缴存</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
//        return success(drugdepositService.getById(id));
        QueryWrapper<DrugdepositVO> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", id);
        List<DrugdepositVO> records = drugdepositService.listPage(new Page<>(), wrapper).getRecords();
        if (records.isEmpty()) {
            return success(null);
        }
        return success(records.get(0));
    }

    /**
     * 添加一个 <strong>医护站药品缴存</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody DrugdepositEntity entity) {
        drugdepositService.saveDrugdeposit(entity);

//        HttpHeaders headers = new HttpHeaders();
//        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
//        return new ResponseEntity<>(headers, HttpStatus.CREATED);
        return success(entity);
    }

    /**
     * 修改一个 <strong>医护站药品缴存</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody DrugdepositEntity entity) {
        entity.setId(id);
        drugdepositService.updateDrugdeposit(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>医护站药品缴存</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        drugdepositService.deleteDrugdeposit(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>医护站药品缴存</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        drugdepositService.deleteDrugdeposit(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<DrugdepositEntity> buildLambdaQuery(DrugdepositEntity entity) {
        LambdaQueryChainWrapper<DrugdepositEntity> lambdaQuery = drugdepositService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), DrugdepositEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldmanId()), DrugdepositEntity::getOldmanId, entity.getOldmanId());
        lambdaQuery.eq(entity.getDepositdate() != null, DrugdepositEntity::getDepositdate, entity.getDepositdate());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDepositmethod()), DrugdepositEntity::getDepositmethod, entity.getDepositmethod());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRegistor()), DrugdepositEntity::getRegistor, entity.getRegistor());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<DrugdepositEntity> buildQuery(DrugdepositVO entity) {
        QueryWrapper<DrugdepositEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanId()), "t1.oldman_id", entity.getOldmanId());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanName()), "t2.oldman_name", entity.getOldmanName());
        wrapper.eq(entity.getDepositdate() != null, "t1.depositdate", entity.getDepositdate());
        wrapper.like(StringUtils.isNotBlank(entity.getDepositmethod()), "t1.depositmethod", entity.getDepositmethod());
        wrapper.like(StringUtils.isNotBlank(entity.getRegistor()), "t1.registor", entity.getRegistor());
        wrapper.like(StringUtils.isNotBlank(entity.getRegistorName()), "t3.name", entity.getRegistorName());
        wrapper.like(StringUtils.isNotBlank(entity.getBedName()), "t4.name", entity.getBedName());
        return wrapper;
    }

}
